package com.dykj.fireFighting.web.maintenance;

import com.dykj.fireFighting.api.base.effecttimesetting.service.IEffectiveTimeSettingService;
import com.dykj.fireFighting.api.base.effecttimesetting.vo.EffectiveTimeSettingVo;
import com.dykj.fireFighting.api.base.enterprise.entity.Enterprise;
import com.dykj.fireFighting.api.base.enterprise.service.IEnterpriseService;
import com.dykj.fireFighting.api.base.maintenance.dto.UrgentMaintenanceDto;
import com.dykj.fireFighting.api.base.maintenance.entity.RepairRecord;
import com.dykj.fireFighting.api.base.maintenance.entity.UrgentMaintenance;
import com.dykj.fireFighting.api.base.maintenance.service.IRepairRecordService;
import com.dykj.fireFighting.api.base.maintenance.service.IUrgentMaintenanceService;
import com.dykj.fireFighting.api.base.maintenance.vo.UrgentMaintenanceStatisticsVo;
import com.dykj.fireFighting.api.base.maintenance.vo.UrgentMaintenanceVo;
import com.dykj.fireFighting.common.core.annotation.AutoLog;
import com.dykj.fireFighting.common.core.api.CommonResult;
import com.dykj.fireFighting.common.core.utils.UUIDUtils;
import com.dykj.fireFighting.common.core.vo.LoginUserVo;
import com.dykj.fireFighting.common.core.vo.PageVo;
import com.dykj.fireFighting.web.base.shiro.ShiroUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.Date;


/**
 * 紧急维保记录接口
 *
 * @Author JacksonTu
 * @Date 2021-07-28 08:38:40
 */
@Slf4j
@Api(value = "紧急维保记录接口", tags = {"紧急维保记录接口"})
@RestController
@RequestMapping("maintenance/urgentMaintenance")
public class UrgentMaintenanceController {

    @DubboReference
    private IUrgentMaintenanceService urgentMaintenanceService;
    @DubboReference
    private IRepairRecordService repairRecordService;
    @DubboReference
    private IEffectiveTimeSettingService iEffectiveTimeSettingService;
    @DubboReference
    private IEnterpriseService enterpriseService;

    /**
     * 紧急维保记录列表
     *
     * @return
     */
    @AutoLog(value = "紧急维保记录-分页列表查询")
    @ApiOperation(value = "紧急维保记录分页列表", notes = "紧急维保记录分页列表")
    @GetMapping("/list")
    @RequiresPermissions("maintenance/urgentMaintenance/list")
    public CommonResult<PageVo<UrgentMaintenanceVo>> queryUrgentMaintenanceList(UrgentMaintenanceDto urgentMaintenanceDto) {
        if (ShiroUtil.getUser().getEnterpriseId() != null) {
            urgentMaintenanceDto.setMaintenanceDept(ShiroUtil.getUser().getEnterpriseId().toString());
        }
        PageVo<UrgentMaintenanceVo> page = urgentMaintenanceService.queryUrgentMaintenanceList(urgentMaintenanceDto);
        return CommonResult.success(page);
    }

    /**
     * 获取紧急维保维修记录信息
     *
     * @param id
     * @return
     */
    @AutoLog(value = "获取紧急维保维修记录信息")
    @ApiOperation(value = "获取紧急维保维修记录信息", notes = "获取紧急维保维修记录信息")
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", dataType = "String", required = true)
    @GetMapping("/info/{id}")
    public CommonResult<UrgentMaintenanceVo> getInfoById(@PathVariable("id") String id) {
        UrgentMaintenanceVo urgentMaintenanceVo = urgentMaintenanceService.getInfoById(id);
        return CommonResult.success(urgentMaintenanceVo);
    }


    /**
     * 获取紧急维保统计信息
     */
    @AutoLog(value = "紧急维保记录-获取紧急维保统计信息")
    @ApiOperation(value = "获取紧急维保统计信息", notes = "获取紧急维保统计信息")
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", dataType = "String", required = true)
    @GetMapping("/statistic")
    public CommonResult<UrgentMaintenanceStatisticsVo> info() {
        LoginUserVo loginUserVo = ShiroUtil.getUser();
        String id = null;
        if (loginUserVo.getUserType() != 0) {
            id = loginUserVo.getEnterpriseId().toString();
        }
        UrgentMaintenanceStatisticsVo urgentMaintenanceStatisticsVo = urgentMaintenanceService.getStatistics(id);
        return CommonResult.success(urgentMaintenanceStatisticsVo);
    }

    /**
     * 保存紧急维保记录信息
     */
    @AutoLog(value = "紧急维保记录-保存紧急维保记录信息")
    @ApiOperation(value = "保存紧急维保记录信息", notes = "保存紧急维保记录信息")
    @PostMapping("/save")
    @RequiresPermissions("maintenance/urgentMaintenance/save")
    public CommonResult save(@Valid @RequestBody RepairRecord repairRecord) {
        try {
            UrgentMaintenance urgentMaintenance = urgentMaintenanceService.getById(repairRecord.getUrgentMaintenanceId());
            LoginUserVo loginUserVo = ShiroUtil.getUser();
            Enterprise enterprise=enterpriseService.getInfoByBindingCode(loginUserVo.getBindingCode());
            int isEffective=1;
            int effectiveTime=0;
            if(null!=enterprise) {
                EffectiveTimeSettingVo effectiveTimeSettingVo = iEffectiveTimeSettingService.isEffective(urgentMaintenance.getCreateDate(), "hiddenDanger",enterprise.getId().toString());
                isEffective = effectiveTimeSettingVo.getIsEffective() == true ? 1 : 0;
                effectiveTime=effectiveTimeSettingVo.getTime();
            }
            urgentMaintenance.setIsEffective(isEffective);
            urgentMaintenance.setHiddenDangerTime(effectiveTime);
            urgentMaintenance.setRectificationDate(new Date());
            String status = repairRecord.getStatus();
            if ("1".equals(status)) {
                urgentMaintenance.setStatus(2);
            } else {
                urgentMaintenance.setStatus(Integer.valueOf(repairRecord.getStatus()));
            }
            urgentMaintenanceService.updateById(urgentMaintenance);

            repairRecord.setId(UUIDUtils.get32UUID());
            repairRecord.setCreateDate(new Date());
            repairRecord.setCreateBy(loginUserVo.getId().toString());
            repairRecord.setHiddenDangerTime(effectiveTime);
            repairRecord.setIsEffective(isEffective);
            repairRecordService.save(repairRecord);

            return CommonResult.success("添加成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }

    /**
     * 修改紧急维保记录信息
     */
    @AutoLog(value = "紧急维保记录-修改紧急维保记录信息")
    @ApiOperation(value = "修改紧急维保记录信息", notes = "修改紧急维保记录信息")
    @PostMapping("/update")
    @RequiresPermissions("maintenance/urgentMaintenance/update")
    public CommonResult update(@Valid @RequestBody UrgentMaintenance urgentMaintenance) {
        try {
            urgentMaintenanceService.updateById(urgentMaintenance);
            return CommonResult.success("修改成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }

    /**
     * 删除紧急维保记录信息
     */
    @AutoLog(value = "紧急维保记录-删除紧急维保记录信息")
    @ApiOperation(value = "删除紧急维保记录信息", notes = "删除紧急维保记录信息")
    @ApiImplicitParam(paramType = "query", name = "ids", value = "ID数组", dataType = "String", required = true, allowMultiple = true)
    @PostMapping("/delete")
    @RequiresPermissions("maintenance/urgentMaintenance/delete")
    public CommonResult delete(@RequestBody String[] ids) {
        try {
            urgentMaintenanceService.removeByIds(Arrays.asList(ids));
            return CommonResult.success("删除成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }


    /**
     * 维保评价
     */
    @AutoLog(value = "维保评价列表")
    @ApiOperation(value = "维保评价列表", notes = "维保评价列表")
    @GetMapping("/queryEvaluationList")
    public CommonResult<PageVo<UrgentMaintenanceVo>> queryEvaluationList(UrgentMaintenanceDto urgentMaintenanceDto) {
        if (ShiroUtil.getUser().getEnterpriseId() != null) {
            urgentMaintenanceDto.setMaintenanceDept(ShiroUtil.getUser().getEnterpriseId().toString());
        }
        PageVo<UrgentMaintenanceVo> page = urgentMaintenanceService.queryEvaluationList(urgentMaintenanceDto);
        return CommonResult.success(page);
    }
}
